//
//  HMNetWorkTools.swift
//  Faner_Weibo
//
//  Created by 范英强 on 16/1/29.
//  Copyright © 2016年 范英强. All rights reserved.
//

import UIKit
import AFNetworking
enum requsetMethod:String {
    case GET = "GET"
    case POST = "POST"
}
class HMNetWorkTools: AFHTTPSessionManager {
    //给闭包起个别名 
    typealias HMNetWorkToolsCallback = (response:AnyObject?,error:NSError?)->()
    static let sharedTools: HMNetWorkTools = {
        let sharedTools = HMNetWorkTools()
        sharedTools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return sharedTools
    }()
    func demo(){
     //   self.GET(<#T##URLString: String##String#>, parameters: <#T##AnyObject?#>, progress: <#T##((NSProgress) -> Void)?##((NSProgress) -> Void)?##(NSProgress) -> Void#>, success: <#T##((NSURLSessionDataTask, AnyObject?) -> Void)?##((NSURLSessionDataTask, AnyObject?) -> Void)?##(NSURLSessionDataTask, AnyObject?) -> Void#>, failure: <#T##((NSURLSessionDataTask?, NSError) -> Void)?##((NSURLSessionDataTask?, NSError) -> Void)?##(NSURLSessionDataTask?, NSError) -> Void#>)
    }
    
    func request(method: requsetMethod,url:String,params:AnyObject?,callback:HMNetWorkToolsCallback){
        
        
        //成功的闭包
        let successCallback = { (dataTask:NSURLSessionDataTask, response:AnyObject?) -> Void in
            callback(response: response, error: nil)

        }
        
        let failurlCallback = { (dataTask:NSURLSessionDataTask?, error:NSError) -> Void in
            callback(response: nil, error: error)
        }
        
        if method == .GET {
            self.GET(url, parameters: params, progress: nil, success: successCallback, failure: failurlCallback)
        }else {
            self.POST(url, parameters: params, progress: nil, success: successCallback, failure: failurlCallback)
        }
    
    }
}

extension HMNetWorkTools{
    func loadAccessToken(code: String,callback:HMNetWorkToolsCallback){
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let params = [
            "client_id" : APP_KEY,
            "client_secret" : APP_SECRET,
            "grant_type" : "authorization_code",
            "code" : code,
            "redirect_uri": REDIRECT_URL
        ]
        request(.POST, url: urlString, params: params) { (response, error) -> () in
            if error != nil{
                print("请求失败\(error)")
                callback(response: nil, error: error)
                return
            }
            callback(response: response, error: nil)
        }
    }
    
    //请求用户的数据
    func loadUserInfo(accessToken:String,uid:String,callback:HMNetWorkToolsCallback){
        let urlString = "https://api.weibo.com/2/users/show.json"
        let params = [
            "access_token":accessToken,
            "uid": uid
        ]
        HMNetWorkTools.sharedTools.request(.GET, url: urlString, params: params) { (response, error) -> () in
            if error != nil{
                 callback(response: nil, error: error)
                return
            }
            callback(response: response, error: nil)
            
        }
    }

}

extension HMNetWorkTools{
    func loadStatusData(accessToken: String,maxid:Int64 = 0 ,callback:HMNetWorkToolsCallback){
        let urlString = "https://api.weibo.com/2/statuses/friends_timeline.json"
        let params = [
            "access_token": accessToken,
            "max_id": "\(maxid)"
        ]
        request(.GET, url: urlString, params: params, callback: callback)
    }
}
